﻿// <copyright file="ImporterHelper.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Import
{
    using System;
    using System.IO;
    using System.Windows.Controls;
    using Microsoft.LightSwitch.Framework.Client;

    public static class ImporterHelper
    {
        private const string CSVFilter = "Comma Separated Value Files (*.csv, *.txt)|*.csv;*.txt";
        private const string ZipFilter = "Compressed Files (*.zip)|*.zip";

        public static void ImportAlignments(DataWorkspace workspace, VisualCollection<Alignment> collection)
        {
            ImportData(
                s => new AlignmentImporter(workspace).ImportData(s, collection),
                CSVFilter);
        }

        public static void ImportArmies(DataWorkspace workspace, VisualCollection<Army> collection)
        {
            ImportData(
                s => new ArmyImporter(workspace).ImportData(s, collection),
                CSVFilter);
        }

        public static void ImportBaseActions(DataWorkspace workspace, VisualCollection<BaseAction> collection)
        {
            ImportData(
                 s => new BaseActionImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportBaseLandUnits(DataWorkspace workspace, VisualCollection<BaseLandUnit> collection)
        {
            ImportData(
                 s => new BaseLandUnitImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportBaseProvinces(DataWorkspace workspace, VisualCollection<BaseProvince> collection)
        {
            ImportData(
                 s => new BaseProvinceImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportBaseSeaUnits(DataWorkspace workspace, VisualCollection<BaseSeaUnit> collection)
        {
            ImportData(
                 s => new BaseSeaUnitImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportBloodAbilities(DataWorkspace workspace, VisualCollection<BloodAbility> collection)
        {
            ImportData(
                 s => new BloodAbilityImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportBloodlineDerivations(DataWorkspace workspace, VisualCollection<BloodlineDerivation> collection)
        {
            ImportData(
                 s => new BloodlineDerivationImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportBloodlineStrengths(DataWorkspace workspace, VisualCollection<BloodlineStrength> collection)
        {
            ImportData(
                 s => new BloodlineStrengthImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportCharacters(DataWorkspace workspace, VisualCollection<Character> collection)
        {
            ImportData(
                 s => new CharacterImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportClasses(DataWorkspace workspace, VisualCollection<CharacterClass> collection)
        {
            ImportData(
                 s => new ClassImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportDeities(DataWorkspace workspace, VisualCollection<Deity> collection)
        {
            ImportData(
                 s => new DeityImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportDomains(DataWorkspace workspace, VisualCollection<Domain> collection)
        {
            ImportData(
                 s => new DomainImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportDomainProvincesData(DataWorkspace workspace)
        {
            ImportData(
                 s => new DomainProvinceImporter(workspace).ImportData(s),
                 CSVFilter);
        }

        public static void ImportDomainTurns(DataWorkspace workspace, VisualCollection<DomainTurn> collection)
        {
            ImportData(
                 s => new DomainTurnImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportDomainActions(DataWorkspace workspace)
        {
            ImportData(
                s => new DomainActionImporter(workspace).ImportData(s),
                CSVFilter);
        }

        public static void ImportGameInformation(DataWorkspace workspace, VisualCollection<GameInformation> collection)
        {
            ImportData(
                 s => new GameInformationImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportLandUnits(DataWorkspace workspace)
        {
            ImportData(
                s => new LandUnitImporter(workspace).ImportData(s),
                CSVFilter);
        }

        public static void ImportNeighbours(DataWorkspace workspace, VisualCollection<BaseProvince> collection)
        {
            ImportData(
                 s => new NeighbourImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportPlayers(DataWorkspace workspace, VisualCollection<Player> collection)
        {
            ImportData(
                 s => new PlayerImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportRaces(DataWorkspace workspace, VisualCollection<Race> collection)
        {
            ImportData(
                 s => new RaceImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportRealmSpells(DataWorkspace workspace, VisualCollection<RealmSpell> collection)
        {
            ImportData(
                 s => new RealmSpellImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportSeaUnits(DataWorkspace workspace)
        {
            ImportData(
                s => new SeaUnitImporter(workspace).ImportData(s),
                CSVFilter);
        }

        public static void ImportSkills(DataWorkspace workspace, VisualCollection<Skill> collection)
        {
            ImportData(
                 s => new SkillImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportTerrains(DataWorkspace workspace, VisualCollection<Terrain> collection)
        {
            ImportData(
                 s => new TerrainImporter(workspace).ImportData(s, collection),
                 CSVFilter);
        }

        public static void ImportFullGameData(DataWorkspace workspace)
        {
            ImportData(
                 s => new GlobalImporter(workspace).ImportData(s),
                 ZipFilter);
        }

        private static void ImportData(Action<Stream> importAction, string dialogFilter)
        {
            Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
            {
                if (System.Windows.Application.Current.IsRunningOutOfBrowser)
                {
                    OpenFileDialog deskTopDialog = new OpenFileDialog();
                    deskTopDialog.Filter = dialogFilter;

                    if (deskTopDialog.ShowDialog() == true)
                    {
                        importAction(deskTopDialog.File.OpenRead());
                    }
                }
                else
                {
                    DialogLauncherWindow webDialog = new DialogLauncherWindow();
                    webDialog.Title = "Open";
                    webDialog.DialogType = DialogType.Open;
                    webDialog.DialogFilter = dialogFilter;
                    webDialog.Closed += (o, e) =>
                        {
                            DialogLauncherWindow temp = (DialogLauncherWindow)o;

                            if (temp.DialogResult == true && (temp.SelectedFile != null))
                            {
                                importAction(temp.SelectedFile);
                            }
                        };
                    webDialog.Show();
                }
            });
        }
    }
}
